/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：AuthenticationFace.java
 * History:
 *       2012-1-18: Initially created, tangkf.
 */
package com.xteam.purview.service.impl;

import java.util.List;

import com.tangkf.utils.CommonUtils;
import com.xteam.db.IBaseDao;
import com.xteam.purview.entities.PurviewRoleFunDatarule;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.entities.PurviewUserFunDatarule;
import com.xteam.purview.entities.PurviewUserRole;
import com.xteam.purview.service.AbsFace;
import com.xteam.purview.service.IAuthorizationFace;

/**
 * 授权接口规范实现 用于实现授权操作的相关接口规范
 * @author tangkf ---2012-1-18
 */
public class AuthorizationFace extends AbsFace<PurviewUser> implements IAuthorizationFace {
	
	private AuthorizationFace() {
	};

	private static AuthorizationFace	instance	= new AuthorizationFace();

	public static AuthorizationFace getInstance() {
		return instance;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#addUserRoleRelation(java.lang.String, java.lang.String)
	 */
	public int addUserRoleRelation(String userId, String[] roleId) {
		int r = 0;
		try {
			if (CommonUtils.notEmpty(userId)) {
				// 1.清空用户与角色的关系
				r = dao.deleteBathById(PurviewUserRole.class, PurviewUserRole.PROP_USERID, new String[] { userId });

				// 2.重新设置用户与角色的关系
				PurviewUserRole puf = null;
				if (roleId != null)
					for (int i = 0; i < roleId.length; i++) {
						puf = new PurviewUserRole();
						// TODO 等待完善对冗余数据 用户名，功能名等的填写
						puf.setUserId(userId);
						puf.setRoleId(roleId[i]);
						dao.saveObject(puf);
					}
			}
		} catch (Exception e) {
			r = 1;
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#getUserRoleRelation(java.lang.String)
	 */
	public List<PurviewUserRole> getUserRoleRelation(String userId) {
		String hql	= IBaseDao.SQL_FROM + PurviewUserRole.REF + IBaseDao.SQL_WHERE + PurviewUserRole.PROP_USERID + "=?";
		List<PurviewUserRole> list	=	null;
		try {
			list	= dao.queryAll(hql, new String[]{userId});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#delUserRoleRelation(java.lang.String, java.lang.String)
	 */
	public int delUserRoleRelation(String userId, String roleId) {
		return 0;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#addUserFunNodeRelation(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int addUserFunNodeRelation(String userId, String[] funNodeId, String typeId) {
		int r = 0;
		try {
			if (CommonUtils.notEmpty(userId)) {
				// 1.清空用户与权限的关系
				r = dao.deleteBathById(PurviewUserFunDatarule.class, PurviewUserFunDatarule.PROP_USERID, new String[] { userId });

				// 2.重新设置用户与权限的关系
				PurviewUserFunDatarule puf = null;
				if (funNodeId != null)
					for (int i = 0; i < funNodeId.length; i++) {
						puf = new PurviewUserFunDatarule();
						// TODO 等待完善对冗余数据 用户名，功能名等的填写
						puf.setUserId(userId);
						puf.setFunId(funNodeId[i]);
						puf.setFunPlist(1);
						dao.saveObject(puf);
					}
			}
		} catch (Exception e) {
			r = 1;
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#getUserFunNodeRelation(java.lang.String)
	 */
	public List<PurviewUserFunDatarule> getUserFunNodeRelation(String userId) {
		String hql	= IBaseDao.SQL_FROM + PurviewUserFunDatarule.REF + IBaseDao.SQL_WHERE + PurviewUserFunDatarule.PROP_USERID + "=?";
		List<PurviewUserFunDatarule> list	=	null;
		try {
			list	= dao.queryAll(hql, new String[]{userId});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#delUserFunNodeRelation(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int delUserFunNodeRelation(String userId, String funNodeId, String typeId) {
		return 0;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#delUserFunNodeRelation(java.lang.String, java.lang.String)
	 */
	public int delUserFunNodeRelation(String userId, String funNodeId) {
		return 0;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#addRoleFunNodeRelation(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int addRoleFunNodeRelation(String roleId, String[] funNodeId, String typeId) {
		int r = 0;
		try {
			if (CommonUtils.notEmpty(roleId)) {
				// 1.清空角色与权限的关系
				r = dao.deleteBathById(PurviewRoleFunDatarule.class, PurviewRoleFunDatarule.PROP_ROLEID, new String[] { roleId });

				// 2.重新设置角色与权限的关系
				PurviewRoleFunDatarule prf = null;
				if (funNodeId != null)
					for (int i = 0; i < funNodeId.length; i++) {
						prf = new PurviewRoleFunDatarule();
						// TODO 等待完善对冗余数据角色名，功能名等的填写
						prf.setRoleId(roleId);
						prf.setFunId(funNodeId[i]);
						prf.setFunPlist(1);
						dao.saveObject(prf);
					}
			}
		} catch (Exception e) {
			r = 1;
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * 读取角色关联的功能节点
	 * @see com.xteam.purview.service.IAuthorizationFace#getRoleFunNodeRelation(java.lang.String)
	 */
	public List<PurviewRoleFunDatarule> getRoleFunNodeRelation(String roleId) {
		String hql	= IBaseDao.SQL_FROM + PurviewRoleFunDatarule.REF + IBaseDao.SQL_WHERE + PurviewRoleFunDatarule.PROP_ROLEID + "=?";
		List<PurviewRoleFunDatarule> list	=	null;
		try {
			list	= dao.queryAll(hql, new String[]{roleId});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#delRoleFunNodeRelation(java.lang.String, java.lang.String, java.lang.String)
	 */
	public int delRoleFunNodeRelation(String roleId, String funNodeId, String typeId) {
		return 0;
	}

	/**
	 * @see com.xteam.purview.service.IAuthorizationFace#delRoleFunNodeRelation(java.lang.String, java.lang.String)
	 */
	public int delRoleFunNodeRelation(String roleId, String funNodeId) {
		return 0;
	}
}